package cn.bxshey.feign.config;


import cn.bxshey.general.constant.JwtConstant;
import cn.bxshey.general.enums.HeaderEnum;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;

/**
 * @作者：不想说话而已
 * @Date：2023/11/30 12:07
 *
 * 设置远程调用前请求头的token
 */
@Configuration
public class FeignHeaderConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {

        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

        //设置远程调用的标识，远程调用则表示不再比对 ip是否符合要求
        requestTemplate.header(HeaderEnum.HEADER_FEIGN.key, HeaderEnum.HEADER_FEIGN.value);

        //设置客户端发送请求的token
        requestTemplate.header(JwtConstant.ACCESS_TOKENS, request.getHeader(JwtConstant.ACCESS_TOKENS));

    }
}
